package com.example.epidemic_assistant;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.android.epidemic_assistant.gen.DaoMaster;
import com.android.epidemic_assistant.gen.DaoSession;
import com.android.epidemic_assistant.gen.InitBeanDao;
import com.android.epidemic_assistant.gen.UserBeanDao;
import com.example.epidemic_assistant.bean.InitBean;
import com.example.epidemic_assistant.bean.UserBean;
import com.example.epidemic_assistant.util.NumberUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 全局类入口，初始化一些数据
 */
public class App extends Application {

    private static final String TAG = "App";

    private static Application mContext;
    private DaoSession daoSession;
    private InitBean mInitBean;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;

        initGreenDao();
    }

    /**
     * greendao数据库初始化
     */
    private void initGreenDao() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "epidemic.db");
        SQLiteDatabase db = devOpenHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();

        UserBeanDao userBeanDao = daoSession.getUserBeanDao();
        InitBeanDao initBeanDao = daoSession.getInitBeanDao();
        List<InitBean> initBeans = initBeanDao.loadAll();
        if (initBeans.size() < 1) {
            mInitBean = new InitBean();
        } else {
            mInitBean = initBeans.get(0);
        }

        //初始化数据
        if (mInitBean.getIsInit()) {
            //初始化一些医护管理员人员信息数据
            UserBean userBean01 = new UserBean();
            userBean01.setIsAdmin(true);
            userBean01.setUserName("admin");
            userBean01.setUserPass("123456");
            userBeanDao.insert(userBean01);

            UserBean userBean02 = new UserBean();
            userBean02.setIsAdmin(true);
            userBean02.setUserName("123456");
            userBean02.setUserPass("123456");
            userBeanDao.insert(userBean02);

            UserBean userBean03 = new UserBean();
            userBean03.setIsAdmin(true);
            userBean03.setUserName("123");
            userBean03.setUserPass("123456789");
            userBeanDao.insert(userBean03);

            UserBean userBean04 = new UserBean();
            userBean04.setIsAdmin(true);
            userBean04.setUserName("assistant");
            userBean04.setUserPass("admin");
            userBeanDao.insert(userBean04);

            //真实数据的体温信息用户
            UserBean userBeans = new UserBean();
            userBeans.setUserName("q123");
            userBeans.setUserPass("a456");
            userBeans.setName("测试名字");
            userBeans.setAge("22");
            userBeans.setSex("男");
            userBeans.setIdCard("233333332333333233333");
            userBeans.setFamilyAddress("测试地址");
            userBeans.setRoomId(22);
            userBeans.setDay(NumberUtil.getRandomNumber(14));
            userBeanDao.insert(userBeans);
        }
        mInitBean.setIsInit(false);
    }

    /**
     * 获取 DaoSession
     *
     * @return DaoSession
     */
    public DaoSession getDaoSession() {
        return daoSession;
    }

    public static Context getApp() {
        return mContext;
    }
}
